<template>
  <div class="mod-config">


    <!--校级 头部 导出区域 -->
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">

      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="项目标题" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <!-- <el-button v-if="isAuth('sys:college:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button> -->
        <el-button v-if="isAuth('sys:college:delete')" type="danger" @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0">批量删除</el-button>
        <!-- <el-button type="primary" size="small" @click="submitNext()">提交给校级</el-button> -->
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="showExcel = true" :disabled="excelData.length == 0">{{ '导出' }}</el-button>
      </el-form-item>
      <el-form-item>

        <el-dialog :visible.sync="showExcel" modal-append-to-body append-to-body width="50%">
          <div>
            <h3>输入excel基本信息</h3>
            <el-form class="excelForm" :label-position="'left'">
              <el-form-item label="excel表标题:">
                <el-input size="small" placeholder="表格标题" style="width:160px" v-model="excelTitle"></el-input>
              </el-form-item>
              <el-form-item label="excel文件名:">
                <el-input size="small" placeholder="文件名" style="width:160px" v-model="excelFName">
                </el-input>
              </el-form-item>
            </el-form>
          </div>
          <div>
            <el-radio-group v-model="excelType">
              <el-radio-button :label="0">{{ '导出项目信息' }}</el-radio-button>
              <el-radio-button :label="1">{{ '导出项目成员信息' }}</el-radio-button>
            </el-radio-group>
          </div>
          <div v-if="excelType == 0">
            <h3>选要导出的字段</h3>
            <el-checkbox :indeterminate="isIndeterminate" v-model="checkExcelAll"
              @change="handleCheckAllChange">全选</el-checkbox>
            <el-checkbox-group v-model="checked_key" @change="handleCheckedKeyChange"
              style="display:flex ; align-items: center;flex-wrap: wrap; justify-content: center;">
              <el-checkbox v-for="k in handleJson_fields" :label="k" :key="k.key">{{ k.name }}</el-checkbox>
            </el-checkbox-group>
          </div>
          <div v-else-if="excelType == 1">
            <h3>选要导出的字段</h3>
            <el-checkbox :indeterminate="isIndeterminate1" v-model="checkExcelAll1"
              @change="handleCheckAllChange1">全选</el-checkbox>
            <el-checkbox-group v-model="checked_key1" @change="handleCheckedKeyChange1"
              style="display:flex ; align-items: center;flex-wrap: wrap; justify-content: center;">
              <el-checkbox v-for="k in handleMember_fields" :label="k" :key="k.key">{{ k.name }}</el-checkbox>
            </el-checkbox-group>
          </div>
          <span slot="footer" class="dialog-footer" style="display: flex;justify-content: flex-end;">
            <el-button @click="showExcel = false">取 消</el-button>
            <!-- <el-button type="primary" @click="showExcel = false">确 定</el-button> -->
            <download-excel :data="excelType == 0 ? excelData : memberData" :fields="excelType == 0 ? header : header1"
              :header="excelTitle" :name="excelFName" :before-generate='beforeGenerate'>
              <el-button
                v-if="(excelTitle != '' && excelFName != '' && checked_key.length != 0) || (excelTitle != '' && excelFName != '' && checked_key1.length != 0)"
                icon="el-icon-download" style="margin: 0 10px;" type="primary">
                {{ "立即导出" }}
              </el-button>
              <el-tag v-else disable-transitions style="margin: 0 0 0 5px; height: 36px;line-height: 36px;">{{
                '填完上面的东西显示导出按钮' }}</el-tag>
            </download-excel>
          </span>
        </el-dialog>

      </el-form-item>
    </el-form>


    <!-- 内容列表区域 -->
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50" fixed="left">
      </el-table-column>
      <el-table-column prop="projectType" header-align="center" align="center" label="比赛类型" width="80" fixed="left">
      </el-table-column>
      <el-table-column prop="type" header-align="center" align="center" label="项目类型" fixed="left">
      </el-table-column>
      <el-table-column prop="college" header-align="center" align="center" label="所属学院" fixed="left">
      </el-table-column>
      <el-table-column prop="title" header-align="center" align="center" label="项目标题" fixed="left" width="250">
      </el-table-column>
      <el-table-column prop="outline" header-align="center" align="center" label="项目概要">
        <template slot-scope="scope">
          <el-tooltip content="查看概要" :open-delay="500">
            <div @click="showOutlineInfo(scope.row.outline)"
              style="display: -webkit-box;-webkit-box-orient: vertical;-webkit-line-clamp: 3;overflow: hidden;">
              {{ scope.row.outline }}
            </div>
          </el-tooltip>
          <el-dialog title="项目概要" append-to-body :visible.sync="showOutline" width="30%">
            <span>{{ outline }}</span>
            <span slot="footer" class="dialog-footer">
              <el-button type="primary" @click="showOutline = false">确 定</el-button>
            </span>
          </el-dialog>
        </template>
      </el-table-column>
      <el-table-column prop="teacherName" header-align="center" align="center" label="指导老师">
      </el-table-column>
      <el-table-column prop="leader" header-align="center" align="center" label="负责人">
        <template slot-scope="scope">
          {{ scope.row.leader }}
        </template>
      </el-table-column>
      <el-table-column prop="phone" header-align="center" align="center" label="负责人联系方式">
        <template slot-scope="scope">
          {{ scope.row.phone }}
        </template>
      </el-table-column>
      <el-table-column prop="average" header-align="center" align="center" label="校评审平均分">
        <template slot-scope="scope">
          <div>{{ scope.row.average ? scope.row.average : 0 }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="peopleNumber" header-align="center" align="center" label="校评分总人数">
        <template slot-scope="scope">
          <div>{{ scope.row.peopleNumber ? scope.row.peopleNumber : 0 }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="noScore" header-align="center" align="center" label="未评分专家名单" width="125">
        <template slot-scope="scope">
          <div v-if="scope.row.noScore.length == 0">无未评分专家</div>
          <el-dropdown v-else>
            <span style="color:#4ea1e1 ;">
              {{ scope.row.noScore.length + '位专家未评' }}<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-for="(k, i) in scope.row.noScore" :key="i" disabled>{{ k }}</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
      <el-table-column prop="level" header-align="center" align="center" label="校推荐意见" width="130">

        <template slot-scope="scope">
          <div v-if="scope.row.statu === 3">{{ '已打回给院级' }}</div>
          <div v-else>
            <div class="tag" v-if="scope.row.level">
              <el-tag effect="dark" v-if="scope.row.level == '校级'">校级</el-tag>
              <el-tag effect="dark" type="success" v-else-if="scope.row.level == '省级'">省级</el-tag>
              <el-tag effect="dark" type="success" v-else-if="scope.row.level == '国家级'">国家级</el-tag>
              <el-tag :effect="'dark'" type="danger" v-else-if="scope.row.level == '不予立项'">不予立项</el-tag>
            </div>
            <el-select placeholder="请选择" v-model="scope.row.level"
              @change="AddOrUpdateLevel(scope.row.level, scope.row.id, scope.row)" v-else>
              <el-option v-for="k in objLevelList" :key="k" :label="k" :value="k">
              </el-option>
            </el-select>
          </div>
        </template>

      </el-table-column>
      <el-table-column prop="ranks" header-align="center" align="center" label="院推荐意见" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.ranks">{{ scope.row.ranks }}</el-tag>
          <div v-else>暂无</div>
        </template>
      </el-table-column>
      <el-table-column prop="collegeAverage" header-align="center" align="center" label="院级评审平均分">
        <template slot-scope="scope">
          <div>{{ scope.row.collegeAverage ? scope.row.collegeAverage : 0 }}</div>
        </template>
      </el-table-column>

      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <div>
            <el-button type="text" size="small" @click="repulse(scope.row.id, scope.row.title)">打回给院级</el-button>
            <!-- <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button> -->
          </div>
          <el-button type="text" size="small" @click="toDetail(scope.row.id)">查看</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>


        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './school-add-or-update'
export default {
  data() {
    return {
      objLevelList: ['校级', '省级', '国家级', '不予立项'],
      status: [{
        id: 0,
        statu: '审核中',
      }, {
        id: 1,
        statu: '通过',
      }, {
        id: 2,
        statu: '不通过',
      },],
      dataForm: {
        key: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      showExcel: false,

      json_fields: {
        // '项目id': 'id',

        '项目类型': 'type',
        '比赛类型': 'projectType',
        '学院': 'college',
        '项目标题': 'title',
        '项目概要': 'outline',
        '指导老师': 'teacherName',
        '负责人': 'leader',
        '负责人联系方式': 'phone',
        '校评审平均分': 'average',
        '校评分总人数': 'peopleNumber',
        '校推荐意见': 'level',
        '院推荐意见': 'ranks',
        '院评审平均分': 'collegeAverage',
      },
      excelTitle: '',
      excelFName: '校级项目表.xls',
      excelData: [],
      ids: [],

      memberJson_fields: {
        '项目标题': 'title',
        '学院': 'college',
        '班级': 'grade',
        '姓名': 'name',
        '登录邮箱': 'mailbox',
        '手机号码': 'phone',
        '身份': 'statuString',
      }, //成员信息excel字段
      // memberData: [], //成员信息,

      json_fields: {
        '项目类型': 'type',
        '比赛类型': 'projectType',
        '学院': 'college',
        '项目标题': 'title',
        '项目概要': 'outline',
        '指导老师': 'teacherName',
        '负责人': 'leader',
        '负责人联系方式': 'phone',
        '校评审平均分': 'average',
        '校评分总人数': 'peopleNumber',
        '校推荐意见': 'level',
        '院推荐意见': 'ranks',
        '院评审平均分': 'collegeAverage',
      },
      memberJson_fields: {
        '项目标题': 'title',
        '学院': 'college',
        '班级': 'grade',
        '姓名': 'name',
        '登录邮箱': 'mailbox',
        '手机号码': 'phone',
        '身份': 'statuString',
      },
      //已选择的字段
      checked_key: [],
      checked_key1: [],
      checkedMember_key: [],
      // 供选择的字段
      handleJson_fields: [
        {
          name: '项目标题',
          key: 'title',
        },
        {
          name: '项目类型',
          key: 'type',
        },
        {
          name: '比赛类型',
          key: 'projectType',
        },
        {
          name: '学院',
          key: 'college',
        },
        {
          name: '项目概要',
          key: 'outline',
        },
        {
          name: '指导老师',
          key: 'teacherName',
        },
        {
          name: '负责人',
          key: 'leader',
        },
        {
          name: '负责人联系方式',
          key: 'phone',
        },
        {
          name: '校评审平均分',
          key: 'average',
        },
        {
          name: '评分人数',
          key: 'peopleNumber',
        },
        {
          name: '校推荐意见',
          key: 'level',
        },
        {
          name: '院推荐意见',
          key: 'ranks',
        },
        {
          name: '院评审平均分',
          key: 'collegeAverage',
        },
        // {
        //   name: '审核状态',
        //   key: 'statu',
        // },
      ],
      handleMember_fields: [
        {
          name: '项目标题',
          key: 'title',
        },
        {
          name: '学院',
          key: 'college',
        },
        {
          name: '班级',
          key: 'grade',
        },
        {
          name: '姓名',
          key: 'name',
        },
        {
          name: '登录邮箱',
          key: 'mailbox',
        },
        {
          name: '手机号码',
          key: 'phone',
        },
        {
          name: '身份',
          key: 'statuString',
        }
      ],
      //导出excel全选
      checkExcelAll: false,
      //导出excel1全选
      checkExcelAll1: false,
      //控制全选框的样式
      isIndeterminate: false,
      //控制全选框1的样式
      isIndeterminate1: false,
      //excel标题
      excelTitle: '',
      //excel文件名
      excelFName: '',
      //导出excel的数据数组
      excelType: 0,//导出数据的类型，0项目信息，1成员信息
      excelData: [],
      ids: [],
      memberData: [],
      showWin: false,//项目重点窗口
      //评审提交的数据
      reviewForm: {
        ranks: '一般项目',
        id: null,
        statu: null,
      },

      //显示概要
      showOutline: false,
      //概要
      outline: '',
    }



  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList()
  },
  computed: {
    excelBtnDis() {
      if ((this.excelTitle !== '') && (this.excelFName !== '') && (this.excelData.length !== 0)) return false
      return true
    },
    header() {
      let obj = {}
      for (let i = 0; i < this.checked_key.length; i++) {
        if (this.checked_key[i].key == 'statu') {
          obj[this.checked_key[i].name] = {
            field: this.checked_key[i].key,
            callback: value => {
              if (value == 0) {
                return '审核中'
              } else if (value == 1) {
                return '通过'
              } else if (value == 2) {
                return '不通过'
              }
            }
          }
        } else {
          obj[this.checked_key[i].name] = this.checked_key[i].key
        }

      }
      return obj
    },
    header1() {
      let obj = {}
      for (let i = 0; i < this.checked_key1.length; i++) {
        obj[this.checked_key1[i].name] = this.checked_key1[i].key
        // if (this.checked_key1[i].key == 'statu') {
        //   obj[this.checked_key1[i].name] = {
        //     field: this.checked_key1[i].key,
        //     callback: value => {
        //       if (value == 0) {
        //         return '审核中'
        //       } else if (value == 1) {
        //         return '通过'
        //       } else if (value == 2) {
        //         return '不通过'
        //       }
        //     }
        //   }
        // } else {
        //   obj[this.checked_key1[i].name] = this.checked_key1[i].key
        // }

      }
      return obj
    }
  },
  methods: {
    showOutlineInfo(text) {
      this.showOutline = true
      this.outline = text
    },
    beforeGenerate() {
      // console.log(e,'excel');//sb插件，这个钩子没有event
      this.$message('开始导出文档')
    },
    //校级打回给院级
    repulse(id, name) {
      let title = '你确定要将该项目打回吗'
      let content = '项目标题：' + name
      this.$confirm(content, title, {
        center: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/school/refer'),
          method: 'post',
          data: this.$http.adornData({
            id,
            statu: 3
          })
        }).then(({
          data
        }) => {
          if (data && data.code == 0) {
            this.$message.success('已打回项目')
            this.getDataList()
          } else {
            this.$message.warning(data.msg)
          }
        })
      }).catch((action) => {
        if (action == 'cancel')
          this.$message.warning('取消打回')
        this.getDataList()
      })



    },
    //评级
    AddOrUpdateLevel(level, id, i) {
      let title = '一旦提交无法修改'
      let content = '你确定要对项目：' + i.title + '评定等级为' + level + '吗'
      this.$confirm(content, title, {
        center: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/school/levelUpdate'),
          method: 'post',
          params: {
            'id': id,
            'level': level,
          }
        }).then(({
          data
        }) => {
          if (data.code == 0) {
            this.$message({
              type: 'success',
              message: '提交成功'
            })
            this.getDataList()
          } else {
            this.$message({
              type: 'error',
              message: res.msg
            })
            this.getDataList()
          }
        })
      }).catch((action) => {
        if (action == 'cancel') {
          console.log('qx');
          this.getDataList()
        }
      })

    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/sys/school/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'key': this.dataForm.key
        })
      }).then(({
        data
      }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
          this.$message.error(data.msg)
        }
        this.dataListLoading = false
      })
    },
    //导出excel
    downExcel() {
      var ids = this.dataListSelections.map(item => {
        return item.id
      })
      this.$http({
        url: this.$http.adornUrl('/sys/school/downexcel'),
        method: 'post',
        data: ids
      }).then(({
        data
      }) => {
        if (data && data.code === 0) {
          this.excelData = data.schoolEntityList
        } else {
          this.$message('数据错误，请联系管理员')
        }

      })
    },
    //导出项目成员信息excel
    downExcelOfMember() {
      var ids = this.dataListSelections.map(item => {
        return item.id
      })
      this.$http({
        url: this.$http.adornUrl('/sys/school/outExcel'),
        method: 'post',
        data: ids
      }).then(({
        data
      }) => {
        this.memberData = data.studentEntities
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
      this.excelData = val
      if (this.dataListSelections.length != 0) {
        this.downExcel()
        this.downExcelOfMember()
      }
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/school/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    isEmtpy() {
      if (this.excelData.length == 0) {
        this.$message.warning('请先选择要导出的项目')
      }
    },
    handleCheckAllChange(val) {
      // let arr = []
      // for (let i = 0; i < this.handleJson_fields.length; i++) {
      //   arr.push(this.handleJson_fields[i].key)
      // }
      // console.log(arr, val);
      this.checked_key = val ? this.handleJson_fields : []
      this.isIndeterminate = false
    },
    // 全选表格1
    handleCheckAllChange1(val) {
      // let arr = []
      // for (let i = 0; i < this.handleJson_fields.length; i++) {
      //   arr.push(this.handleJson_fields[i].key)
      // }
      // console.log(arr, val);
      this.checked_key1 = val ? this.handleMember_fields : []
      this.isIndeterminate1 = false
    },
    handleCheckedKeyChange(value) {
      // console.log(value);
      let checkedCount = value.length
      this.checkExcelAll = checkedCount === this.handleJson_fields.length
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.handleJson_fields.length
    },
    handleCheckedKeyChange1(value) {
      // console.log(value);
      let checkedCount = value.length
      this.checkExcelAll1 = checkedCount === this.handleMember_fields.length
      this.isIndeterminate1 = checkedCount > 0 && checkedCount < this.handleMember_fields.length
    },
    toDetail(val) {
      console.log(val)
      this.$router.push({ name: 'project_detail', query: { id: val } })
    }
  }
}
</script>


<style lang="scss">
.getExcel {
  display: flex;
  flex-direction: row-reverse;
  align-items: center;
  // background-color: #000;
  // justify-content: space-between;
  // margin: 10px 0;
  background-color: #fff;
  border-radius: 15px;
  padding: 0px 20px;
  box-sizing: border-box;

  .excelForm {
    // display: flex;
    // padding: 10px 0;
    // height: 60px;
    // background-color: #f2f3f5;
    box-sizing: border-box;
    width: 600px;

  }
}
</style>
